#include "include.h"
#include "client.h"
#include "server.h"
#include "book.h"
#include "message.h"

int main(int argc, char** argv)
{
        char name[SIZENAME];
        pthread_t client_id, server_id;
        
        /* Arguments handling */
        if (argc != 2)
        {
                printf("Usage: %s <name>\n", argv[0]);
                exit(0);
        }
        
        /* Want some help ? */
        if (strcmp(argv[1], "-help") == 0) 
        {
                printHelp();
                exit(0);
        }
        
        /* Checking name length */
        if (strlen(argv[1]) >= SIZENAME)
        {
                printf("! Your name is too long.\n");
                exit(0);
        }
                
        /* Cleaning & copying name */
        memset(name, '\0', SIZENAME);
        strcpy(name, argv[1]);
        
        /* Let's begin ! */
        msgReceived = NULL;
        
        /* Retrieving user informations */
        openBook(name);
        
        /* Launching client side */
        if ((pthread_create(&client_id, NULL, client_side, (void*) name)) > 0)
        {
                perror("Threading error");
                exit(0);
        }
        
        /* Launching server side */
        /*if ((pthread_create(&server_id, NULL, server_side, NULL)) > 0)
        {
                perror("Threading error");
                exit(0);
        }
        */
        
        registerrpc(/* prognum */ PROGNUM,
                        /* versnum */ VERSNUM,
                        /* procnum */ PROCNUM,
                        /* pointer on function */ serveur,
                        /* argument decoding */ (xdrproc_t)xdr_message,
                        /* function result encoding */ (xdrproc_t)xdr_int);

        svc_run();              /* server ready for clients */
        
        /* Waiting threads */
        pthread_join(client_id, NULL);
        //pthread_join(server_id, NULL);
        
        /* No man's land */     
        return 0;
}

